import React, { Component } from 'react'

import { Switch, Route, Redirect } from 'react-router-dom'
import adminRoutes from '../../routes/index.js'
import AdminLayout from 'components/AdminLayout/index.js'
export default class Admin extends Component {
    render() {
      return (
        <AdminLayout>
        <Switch>
          {
          adminRoutes.map(route=>{
                return (
                  <Route
                    key={route.path}
                    path={route.path}
                    render={(routeProps)=>{
                       // 判断当前路由的 meta roles中是否包含 当前用户的 role 
                       const role = localStorage.getItem('role')
                       const hasPermission = route.meta.roles.some(routeRole=>routeRole===role)
                       if(hasPermission){
                         // 可访问
                         return <route.component {...routeProps}/>
                       }else{
                         return <Redirect to="/admin/nopermission" />
                       }
                    }}
                  />
                )
              })
            }
          <Redirect to="/admin/dashBoard" from="/admin" exact/>
        </Switch>
        </AdminLayout>
      );
    }
}


